#include "form.h"
#include "ui_form.h"
#include<QKeyEvent>
#include"mouse.h"
#include"mouse2.h"
#include"wo.h"
#include"wo2.h"
#include"mouse4.h"

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    keyRespondTimer = new QTimer(this);
    connect(keyRespondTimer,&QTimer::timeout,this,&Form::slotTimeOut);
    QTimer *timer1=new QTimer(this);
           QTimer *timer2=new QTimer(this);
           QTimer *timer3=new QTimer(this);
           QTimer *timer4=new QTimer(this);
           QTimer *timer5=new QTimer(this);
           QTimer *timer6=new QTimer(this);
           QTimer *timer7=new QTimer(this);
           QTimer *timer8=new QTimer(this);
           QTimer* timer9=new QTimer(this);
           QTimer *timer10=new QTimer(this);
           QTimer *timer11=new QTimer(this);
           QTimer *timer12=new QTimer(this);
           QTimer* timer13=new QTimer(this);
           timer1->start(5);
           timer2->start(15);
           timer3->start(10);
           timer4->start(11);
           timer5->start(5);
           timer6->start(10);
           timer7->start(15);
           timer8->start(11);
           timer9->start(4);
           timer10->start(4);
           timer11->start(10);
           timer12->start(12);
           timer13->start(15);
           connect(timer1,&QTimer::timeout,this,&Form::timerevent1);
           connect(timer2,&QTimer::timeout,this,&Form::timerevent2);
           connect(timer3,&QTimer::timeout,this,&Form::timerevent3);
           connect(timer4,&QTimer::timeout,this,&Form::timerevent4);
           connect(timer5,&QTimer::timeout,this,&Form::timerevent5);
           connect(timer6,&QTimer::timeout,this,&Form::timerevent6);
           connect(timer7,&QTimer::timeout,this,&Form::timerevent7);
           connect(timer8,&QTimer::timeout,this,&Form::timerevent8);
           connect(timer1,&QTimer::timeout,this,Form::timerevent9);
           connect(timer2,&QTimer::timeout,this,&Form::timerevent10);
           connect(timer3,&QTimer::timeout,this,&Form::timerevent11);
           connect(timer4,&QTimer::timeout,this,&Form::timerevent12);
           connect(timer5,&QTimer::timeout,this,&Form::timerevent13);
           connect(timer6,&QTimer::timeout,this,&Form::timerevent14);
           connect(timer7,&QTimer::timeout,this,&Form::timerevent15);
           connect(timer8,&QTimer::timeout,this,&Form::timerevent16);
           connect(timer9,&QTimer::timeout,this,&Form::timerevent17);
           connect(timer10,&QTimer::timeout,this,&Form::timerevent18);
           connect(timer11,&QTimer::timeout,this,&Form::timerevent19);
           connect(timer12,&QTimer::timeout,this,&Form::timerevent20);
           connect(timer13,&QTimer::timeout,this,&Form::timerevent21);
           connect(timer10,&QTimer::timeout,this,&Form::timerevent22);
           connect(timer12,&QTimer::timeout,this,&Form::timerevent23);
           connect(timer11,&QTimer::timeout,this,&Form::timerevent24);
           connect(timer13,&QTimer::timeout,this,&Form::timerevent25);

 m=new mouse();
 m->m_mouse.setParent(this);
 m->m_mouse.show();
 m->m_mouse.raise();
 m2=new mouse2();
 m2->m2_mouse.setParent(this);
 m2->m2_mouse.show();
 m2->m2_mouse.raise();
 w=new wo();
 w->w_wo.setParent(this);
 w->w_wo.show();
 w->w_wo.raise();
 m3=new mouse3();
 m3->m3_mouse.setParent(this);
 m3->m3_mouse.show();
 m3->m3_mouse.raise();
 w2=new wo2();
 w2->w2_wo2.setParent(this);
 w2->w2_wo2.show();
 w2->w2_wo2.raise();

 m4=new mouse4();
 m4->m4_mouse.setParent(this);
 m4->m4_mouse.show();
 m4->m4_mouse.raise();


}



Form::~Form()
{

    delete keyRespondTimer;
    delete ui;
    delete m;
    delete m2;
    delete m3;
    delete m4;

}
void Form::keyPressEvent(QKeyEvent *event){
    if(!event->isAutoRepeat())
        keys.append(event->key());
    if(!keyRespondTimer->isActive())
        keyRespondTimer->start(4);



}
void Form::keyReleaseEvent(QKeyEvent *event){
    if(!event->isAutoRepeat())
        keys.removeAll(event->key());
    if(keys.isEmpty())
        keyRespondTimer->stop();
}
void Form::slotTimeOut(){
  foreach(int key,keys){
      switch(key){
      case Qt::Key_Down:
        ui->label1->move(ui->label1->x(),ui->label1->y()+1);
          break;
      case Qt::Key_Left:
          ui->label1->move(ui->label1->x()-1,ui->label1->y());
          break;
      case Qt::Key_Up:
          ui->label1->move(ui->label1->x(),ui->label1->y()-1);
          break;
      case Qt::Key_Right:
          ui->label1->move(ui->label1->x()+1,ui->label1->y());
          break;
      default:
          break;
      }
  }
}

void Form::timerevent1(){int a=ui->label2->pos().x();
                           ui->label2->setGeometry(a+1,ui->label2->pos().y(),30,30);}
void Form::timerevent2(){int a=ui->label2->pos().y();
                           ui->label2->setGeometry(ui->label2->pos().x(),a-1,30,30);}

void Form::timerevent3(){int a=ui->label2->pos().y();
                           ui->label2->setGeometry(ui->label2->pos().x(),a+1,30,30);}
void Form::timerevent4(){int a=ui->label2->pos().x();
                           ui->label2->setGeometry(a-1,ui->label2->pos().y(),30,30);}
void Form::timerevent5(){int a=ui->label3->pos().x();
                           ui->label3->setGeometry(a+1,ui->label3->pos().y(),30,30);}
void Form::timerevent6(){int a=ui->label3->pos().y();
                           ui->label3->setGeometry(ui->label3->pos().x(),a-1,30,30);}
void Form::timerevent7(){int a=ui->label3->pos().y();
                           ui->label3->setGeometry(ui->label3->pos().x(),a+1,30,30);}
void Form::timerevent8(){int a=ui->label3->pos().x();
                           ui->label3->setGeometry(a-1,ui->label3->pos().y(),30,30);}

void Form::check(){
    if((m->m_mouse.x()-ui->label1->x()>-30&&m->m_mouse.x()-ui->label1->x()<45)&&(m->m_mouse.y()-ui->label1->y()>-30&&m->m_mouse.y()-ui->label1->y()<45))
      { m->m_free=true;}
    if(m->m_mouse.x()>2000||m->m_mouse.y()<0||m->m_mouse.y()>1000||m->m_mouse.x()<0)m->m_free=true;
    if((m2->m2_mouse.x()-ui->label1->x()>-30&&m2->m2_mouse.x()-ui->label1->x()<45)&&(m2->m2_mouse.y()-ui->label1->y()>-30&&m2->m2_mouse.y()-ui->label1->y()<45))
      { m2->m2_free=true;}
    if(m2->m2_mouse.x()>2000||m2->m2_mouse.y()<0||m2->m2_mouse.y()>1000||m2->m2_mouse.x()<0)m2->m2_free=true;
    if((m3->m3_mouse.x()-ui->label1->x()>-30&&m3->m3_mouse.x()-ui->label1->x()<45)&&(m3->m3_mouse.y()-ui->label1->y()>-30&&m3->m3_mouse.y()-ui->label1->y()<45))
      { m3->m3_free=true;}
    if(m3->m3_mouse.x()>2000||m3->m3_mouse.y()<0||m3->m3_mouse.y()>1000||m3->m3_mouse.x()<0)m3->m3_free=true;
    if((m4->m4_mouse.x()-ui->label1->x()>-30&&m4->m4_mouse.x()-ui->label1->x()<45)&&(m4->m4_mouse.y()-ui->label1->y()>-30&&m4->m4_mouse.y()-ui->label1->y()<45))
      { m4->m4_free=true;}
    if(m4->m4_mouse.x()>2000||m4->m4_mouse.y()<0||m4->m4_mouse.y()>1000||m4->m4_mouse.x()<0)m4->m4_free=true;
}
void Form::timerevent9(){int a=m->m_mouse.x();
                          m->m_mouse.setGeometry(a+1,m->m_mouse.y(),30,30);}

void Form::timerevent10(){int a=m->m_mouse.y();
                          m->m_mouse.setGeometry(m->m_mouse.x(),a-1,30,30);}
void Form::timerevent11(){int a=m->m_mouse.y();
                          m->m_mouse.setGeometry(m->m_mouse.x(),a+1,30,30);}
void Form::timerevent12(){int a=m->m_mouse.x();
                          m->m_mouse.setGeometry(a-1,m->m_mouse.y(),30,30);}

void Form::timerevent13(){int a=m2->m2_mouse.x();
                          m2->m2_mouse.setGeometry(a+1,m2->m2_mouse.y(),30,30);}

void Form::timerevent14(){int a=m2->m2_mouse.y();
                          m2->m2_mouse.setGeometry(m2->m2_mouse.x(),a-1,30,30);}
void Form::timerevent15(){int a=m2->m2_mouse.y();
                          m2->m2_mouse.setGeometry(m2->m2_mouse.x(),a+1,30,30);}
void Form::timerevent16(){int a=m2->m2_mouse.x();
                          m2->m2_mouse.setGeometry(a-1,m2->m2_mouse.y(),30,30);}
void Form::timerevent18(){int a=m3->m3_mouse.x();
                          m3->m3_mouse.setGeometry(a-1,m3->m3_mouse.y(),30,30);}

void Form::timerevent19(){int a=m3->m3_mouse.y();
                          m3->m3_mouse.setGeometry(m3->m3_mouse.x(),a+1,30,30);}
void Form::timerevent20(){int a=m3->m3_mouse.y();
                          m3->m3_mouse.setGeometry(m3->m3_mouse.x(),a-1,30,30);}
void Form::timerevent21(){int a=m3->m3_mouse.x();
                          m3->m3_mouse.setGeometry(a+1,m3->m3_mouse.y(),30,30);}
void Form::timerevent22(){int a=m4->m4_mouse.x();
                          m4->m4_mouse.setGeometry(a-1,m4->m4_mouse.y(),30,30);}

void Form::timerevent23(){int a=m4->m4_mouse.y();
                          m4->m4_mouse.setGeometry(m4->m4_mouse.x(),a+1,30,30);}
void Form::timerevent24(){int a=m4->m4_mouse.y();
                          m4->m4_mouse.setGeometry(m4->m4_mouse.x(),a-1,30,30);}
void Form::timerevent25(){int a=m4->m4_mouse.x();
                          m4->m4_mouse.setGeometry(a+1,m4->m4_mouse.y(),30,30);}
void Form::timerevent17(){if((ui->label2->x()-ui->label1->x()>-30&&ui->label2->x()-ui->label1->x()<45)&&(ui->label2->y()-ui->label1->y()>-30&&ui->label2->y()-ui->label1->y()<45))ui->label2->hide();
    if((ui->label3->x()-ui->label1->x()>-30&&ui->label3->x()-ui->label1->x()<45)&&(ui->label3->y()-ui->label1->y()>-30&&ui->label3->y()-ui->label1->y()<45))ui->label3->hide();
    if((m->m_mouse.x()-ui->label1->x()>-30&&m->m_mouse.x()-ui->label1->x()<45)&&(m->m_mouse.y()-ui->label1->y()>-30&&m->m_mouse.y()-ui->label1->y()<45))
      m->m_mouse.hide();
     if(m->m_mouse.x()>2000||m->m_mouse.y()<0||m->m_mouse.y()>1000||m->m_mouse.x()<0)m->m_mouse.hide();
    if((m2->m2_mouse.x()-ui->label1->x()>-30&&m2->m2_mouse.x()-ui->label1->x()<45)&&(m2->m2_mouse.y()-ui->label1->y()>-30&&m2->m2_mouse.y()-ui->label1->y()<45))
       m2->m2_mouse.hide();
     if(m2->m2_mouse.x()>2000||m2->m2_mouse.y()<0||m2->m2_mouse.y()>1000||m2->m2_mouse.x()<0)m2->m2_mouse.hide();
     if((m3->m3_mouse.x()-ui->label1->x()>-30&&m3->m3_mouse.x()-ui->label1->x()<45)&&(m3->m3_mouse.y()-ui->label1->y()>-30&&m3->m3_mouse.y()-ui->label1->y()<45))
        m3->m3_mouse.hide();
      if(m3->m3_mouse.x()>2000||m3->m3_mouse.y()<0||m3->m3_mouse.y()>1000||m3->m3_mouse.x()<0)m3->m3_mouse.hide();
      if((m4->m4_mouse.x()-ui->label1->x()>-30&&m4->m4_mouse.x()-ui->label1->x()<45)&&(m4->m4_mouse.y()-ui->label1->y()>-30&&m4->m4_mouse.y()-ui->label1->y()<45))
         m4->m4_mouse.hide();
       if(m4->m4_mouse.x()>2000||m4->m4_mouse.y()<0||m4->m4_mouse.y()>1000||m4->m4_mouse.x()<0)m4->m4_mouse.hide();
    check();
if(m->m_free){
    m->m_free=false;
    m=new mouse();
    m->m_mouse.setParent(this);
    m->m_mouse.show();
    m->m_mouse.raise();}

if(m2->m2_free){
    m2->m2_free=false;
    m2=new mouse2();
    m2->m2_mouse.setParent(this);
    m2->m2_mouse.show();
    m2->m2_mouse.raise();}
if(m3->m3_free){
    m3->m3_free=false;
    m3=new mouse3();
    m3->m3_mouse.setParent(this);
    m3->m3_mouse.show();
    m3->m3_mouse.raise();}
if(m4->m4_free){
    m4->m4_free=false;
    m4=new mouse4();
    m4->m4_mouse.setParent(this);
    m4->m4_mouse.show();
    m4->m4_mouse.raise();}
}


